<template>
  <div class="login">
    <span class="close-btn">
      <i class="iconicon-test iconfont"></i>
    </span>
    <!-- 2.0 Logo 图标 -->
    <h2 class="logo">
      <i class="iconnew iconfont"></i>
    </h2>
    <Mylnput
      v-model="user.username"
      placeholder="用户名/手机号码"
      :rules="rules.username"
      msg="手机号码不合法"
    ></Mylnput>
    <Mylnput
      v-model="user.password"
      type="password"
      :rules="rules.password"
      placeholder="密码"
      msg="密码不合法"
    ></Mylnput>
    <!-- 4.0 一个注册提示 -->
    <div class="tips">
      没有账号？去 <router-link to="/register">注册</router-link>
    </div>
    <!-- 5.0 登录按钮 -->
    <MyButton @click="loginFn" type="danger" style="background-color: blue"
      >登录</MyButton
    >
  </div>
</template>

<script>
import MyButton from "../components/MyButton.vue";
import Mylnput from "../components/Mylnput.vue";
import { userLoginAPI } from "../apis/user";
import { setToken, setUserId } from "../utils/localData";
export default {
  data() {
    return {
      user: {
        username: "12345678911",
        password: "654321",
      },
      rules: {
        username: /^1\d{10}$|^[a-zA-Z]\w{2,10}$|^1\d{4,5}$/,
        password: /^[a-zA-Z0-9~!@#$%^&*()_+=]{3,18}$/,
      },
    };
  },
  components: {
    // 注册子组件的导入名字
    MyButton,
    Mylnput,
  },
  methods: {
    loginFn() {
      // 登录按钮点击时也需要用正则校验
      if (
        this.rules.username.test(this.user.username) &&
        this.rules.password.test(this.user.password)
      ) {
        // 校验通过就发送请求登录
        userLoginAPI({
          // 💥 请求接口参数的键名称，必须按接口文档要求来
          username: this.user.username,
          password: this.user.password,
        }).then((res) => {
          console.log(res);
          // res.data.message
          // res.data.data.token
          const { message } = res.data;
          if (message === "登录成功") {
            // 提取 token
            const { token } = res.data.data;
            // 保存到本地存储中，后续用户信息中心需要用到
            setToken(token);
            setUserId(res.data.data.user.id);

            // 跳转到个人中心页，并传递 id 参数
            // console.log(res.data.data.user.id);
            const from = this.$route.params.from;
            console.log("从哪里进来的登录页", { from });
            /* 如果有 from 表示从某个页面过来的 */
            if (from) {
              // 直接修改地址栏地址，去到某个页面
              location.href = from;
            } else {
              // 没有来源的页面，直接去个人中心
              this.$router.push({
                path: "/user",
                query: { id: res.data.data.user.id },
              });
            }
            // 成功提示 success()
            this.$toast.success(message);
          } else {
            // 失败提示 fail()
            this.$toast.fail("用户名或密码错误");
          }
        });
      } else {
        // alert("用户名或密码校验失败");
        this.$toast("用户名或密码校验失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.close-btn {
  position: fixed;
  left: 20px;
  top: 20px;
  .iconfont {
    font-size: 20px;
    color: #666;
  }
}
.logo {
  display: flex;
  justify-content: center;
  padding: 80px 0 20px;
  .iconfont {
    color: #d81e06;
    font-size: 120px;
  }
}
.tips {
  padding: 0 30px;
  text-align: right;
}
</style>